<template>
  <div class="commentBox">
    <div class="publishComment clearfix">
      <div class="loginBox">
        <div class="login" v-if="!comment.username">
          <router-link to="/login">登录</router-link>
        </div>
        <div class="login" v-else>{{comment.username}}:</div>
      </div>
      <textarea v-model="comment.content" class="writeComment"></textarea>
      <div class="publish" @click="addComments">发表评论</div>
    </div>
    <div class="commentContent">
      <div class="header">
        <div class="title">评论</div>
        <div class="NumberPieces">
          总共 <span>{{newsComments.length}}</span> 条评论
        </div>
      </div>
      <ul class="comment">
        <li v-for="item in newsComments" class="commentItem" :key="item._id">
          <div class="avatar el-icon-user-solid"></div>
          <div class="info">
            <div class="username">{{item.username}}:</div>
            <div class="commentText">{{item.content}}</div>
          </div>
          <div class="time">{{item.date}}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    name: "Comment",
    props: {
      singleNews: {type: Object}
    },
    data() {
      return {
        comment: {
          username: '',
          content: '',
          nid: '',
          title: '',
          uid: '',
          topic: '',
        },
        newsId: ''
      }
    },
    methods: {
      getList() {
        this.$store.dispatch('searchNewsComment', this.newsId)
      },
      addComments() {
        if (!this.comment.username) return this.$message.error('您还未登录请先登录')
        this.$store.dispatch('addComments', this.comment)
          .then(() => {
            this.comment.content = ''
            this.getList()
          })
      },
    },
    created() {
      if(window.sessionStorage.getItem('userInfo')){
        this.comment.uid = JSON.parse(window.sessionStorage.getItem('userInfo'))._id
        this.comment.username = JSON.parse(window.sessionStorage.getItem('userInfo')).username
      }
      this.newsId = this.$route.query.id
      this.getList()
    },
    computed: {
      ...mapState(['newsComments'])
    },
    watch: {
      '$route.query.id'() {
        this.getList()
      },
      singleNews() {
        this.comment.nid = this.singleNews._id
        this.comment.title = this.singleNews.title
        this.comment.topic = this.singleNews.topic
      }
    }
  }
</script>

<style scoped lang="less">
  @color: #1479D7;
  .commentBox {
    border-top: 5px solid @color;
    border-radius: 3px;
    margin-top: 30px;
    padding-top: 50px;

    .publishComment {
      position: relative;

      .loginBox {
        display: block;
        content: '';
        width: 60px;
        height: 60px;
        border-bottom: 2px solid @color;
        border-radius: 50%;
        z-index: 10;
        background: white;
        position: absolute;
        left: 30px;
        top: -36px;

        .login {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          border: 1px solid @color;
          text-align: center;
          line-height: 50px;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          background: white;
          color: #1479D7;
          z-index: 100;

          a {
            color: @color;
          }
        }
      }

      .writeComment {
        width: 100%;
        resize: none;
        border: 2px solid @color;
        border-radius: 8px;
        padding: 25px 20px 0 20px;
        height: 150px;
        z-index: 1;
        position: relative;
      }

      .publish {
        width: 100px;
        height: 34px;
        background: @color;
        color: white;
        text-align: center;
        line-height: 34px;
        border-radius: 8px;
        margin-top: 10px;
        float: right;
        position: relative;
        cursor: pointer;
      }

      .publish:before {
        display: block;
        content: '';
        width: 0;
        height: 0;
        border-width: 10px;
        border-color: transparent transparent @color transparent;
        border-style: solid;
        position: absolute;
        left: -10px;
        bottom: 0;
      }
    }

    .commentContent {

      .header {
        border-bottom: 2px solid @color;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 20px 5px 20px;

        .title {
          font-size: 20px;
          color: @color;
          border-left: 5px solid #25a4bb;
        }

        .NumberPieces {
          font-size: 16px;
          color: @color;

          span {
            font-size: 22px;
            color: #13ce66;
          }
        }
      }

      .comment {
        margin-top: 20px;

        .commentItem {
          display: flex;
          align-items: center;
          border-bottom: 1px solid #ccc;

          .avatar {
            flex: 1;
            font-size: 40px;
            text-align: center;
          }

          .info {
            flex: 8;
            padding: 20px 30px;

            .username {
              font-size: 16px;
              color: #666666;
              margin-bottom: 20px;
            }

            .commentText {
              font-size: 18px;
            }
          }

          .time {
            flex: 3;
            text-align: right;
          }
        }
      }
    }
  }

  .clearfix:after {
    content: "";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
  }

  .clearfix {
    /* 触发 hasLayout */
    zoom: 1;
  }

</style>